package com.xuxk.project.util;

import org.apache.commons.lang3.StringUtils;
import java.util.Random;
import java.util.UUID;

/**
 * 橙云科技有限公司
 *
 * @author 许孝康
 * @date 2019-04-12
 */
public class UUIDGenerator {

	public UUIDGenerator() {
	}

	/**
	 * 获得一个UUID
	 * 
	 * @return String UUID
	 */
	public static String getUUID() {
		String s = UUID.randomUUID().toString();
		// 去掉“-”符号
		return s.replaceAll("-", "");
	}

	/**
	 * 根据指定前缀创建唯一性id
	 * @param ipAddress
	 * @return
	 */
	public static String bulidPrimaryKey(String ipAddress){

		//获取UUID
		int randomNum = new Random().nextInt(999) % 900 + 100;
		String uuid =  getUUID().toUpperCase()+randomNum;
		if(StringUtils.isNotBlank(ipAddress)){
			uuid =  ipAddress + "$" + uuid;
		}

		//生成后缀
		long suffix = Math.abs(uuid.hashCode() % 100000000);
		String time = DateUtil.getDate(DateUtil.currentTime(),"yyyyMMddHHmmss");
		//生成前缀
		long prefix = Long.parseLong(time) * 100000000;
		String primaryKey =  String.valueOf(prefix + suffix);
		return primaryKey;
	}
	public static void main(String[] args) {
		System.out.println(bulidPrimaryKey(""));
	}
}
